package com.example.basemodel.utils

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Point
import android.os.Build
import android.util.TypedValue
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import com.gyf.immersionbar.ImmersionBar
import com.kt.NetworkModel.utils.MLog

/**
 *@author: Administrator
 *2023/4/10
 *desc:
 */
object StatusBarUtils {
    /**
     * 修改状态栏为全透明
     *
     * @param activity
     */
    fun transparencyBar(activity: Activity) {
//        val decorView = activity.window.decorView
//        val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//        decorView.systemUiVisibility = option
//        activity.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
//        activity.window.statusBarColor = Color.TRANSPARENT

        ImmersionBar.with(activity)
            .fitsSystemWindows(false) //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
            .init() //必须调用方可应用以上所配置的参数
    }

    /**
     * 获取状态栏高度
     *
     * @param activity
     * @return
     */
    fun getStatusBarHeight(activity: Activity?): Int {
        //判断是不是 刘海屏
        return if (ImmersionBar.hasNotchScreen(activity!!)) {
            MLog.d("刘海屏 状态栏高度--->  " + ImmersionBar.getNotchHeight(activity))
            ImmersionBar.getNotchHeight(activity)
        } else {
            MLog.d("状态栏高度--->  " + ImmersionBar.getStatusBarHeight(activity))
            ImmersionBar.getStatusBarHeight(activity)
        }
    }

    /**
     * 设置当前View 是状态栏 高度
     *
     * @param topRoot  需要设置的View
     * @param activity 当前的 activity
     */
    fun setStatusBarViewHeight(topRoot: View, activity: Activity?) {
        //获取状态栏高度  把去掉的 状态栏高度设置
        val params = topRoot.layoutParams
        params.height = getStatusBarHeight(activity)
        topRoot.layoutParams = params
    }

    /**
     * 获取 屏幕的高度
     *
     * @param activity
     * @return
     */
    fun getActivityHeight(activity: Activity): Int {
        val defaultDisplay = activity.windowManager.defaultDisplay
        val point = Point()
        defaultDisplay.getSize(point)
        val x = point.x
        return point.y
    }

    /**
     * 获取 屏幕的高度
     *
     * @param activity
     * @return
     */
    fun getActivityWidth(activity: Activity): Int {
        val defaultDisplay = activity.windowManager.defaultDisplay
        val point = Point()
        defaultDisplay.getSize(point)
        val x = point.x
        val y = point.y
        return x
    }


    /**
     * 获取 宽高比
     */
    fun getRatioHeight(activity: Activity, width: Int, ratio: Float): Float {
        val defaultDisplay = activity.windowManager.defaultDisplay
        val point = Point()
        defaultDisplay.getSize(point)
        val x = point.x
        val y = point.y
        return if (width == 0) x * ratio else width * ratio
    }

//    fun dp2Px(context: Context?, value: Float): Float {
//        return if (context == null) {
//            0.0f
//        } else {
//            val typedValue = TypedValue()
//            val metrics = context.resources.displayMetrics
//            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, metrics)
//        }
//    }


    fun sp2Px(pxValue: Float): Float {
        return pxValue / Resources.getSystem().displayMetrics.density
    }

    fun dpToPx(dpValue: Float): Int {
        return (0.5f + dpValue * Resources.getSystem().displayMetrics.density).toInt()
    }

    /**
     * 设置View 宽高比
     *
     * @param c
     * @param v
     * @param ratio
     */
    fun setViewRatio(c: Activity?, v: View, ratio: Float) {
        val width = getActivityWidth(c!!)
        v.layoutParams = ConstraintLayout.LayoutParams(width, (width * ratio).toInt())
    }

    fun hideSystemWindows(activity: Activity?, isHide: Boolean) {
        ImmersionBar.with(activity!!)
            .fitsSystemWindows(isHide) //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
            .init() //必须调用方可应用以上所配置的参数
    }

    fun setBackMarginTop(activity: Activity?, v: View, addHeight: Float = 0.0f) {
        if (activity == null) return
        val statusBarHeight = getStatusBarHeight(activity)
        if (v.layoutParams is ConstraintLayout.LayoutParams) {
            val layoutParams = v.layoutParams as ConstraintLayout.LayoutParams
            layoutParams.topMargin = statusBarHeight + dpToPx(addHeight)
            v.layoutParams = layoutParams
        } else if (v.layoutParams is LinearLayout.LayoutParams) {
            val layoutParams = v.layoutParams as LinearLayout.LayoutParams
            layoutParams.topMargin = statusBarHeight + dpToPx(addHeight)
            v.layoutParams = layoutParams
        }
    }

    /**
     * @param activity
     * @param v
     * @param isDarkFont true 深色 文字颜色  true 黑色 false 白色
     */
    fun setBackMarginTop(activity: Activity, v: View, isDarkFont: Boolean) {
        ImmersionBar.with(activity)
            .statusBarDarkFont(isDarkFont)
            .fitsSystemWindows(false) //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
            .init() //必须调用方可应用以上所配置的参数
        val statusBarHeight = getStatusBarHeight(activity)
        if (v.layoutParams is ConstraintLayout.LayoutParams) {
            val layoutParams = v.layoutParams as ConstraintLayout.LayoutParams
            layoutParams.topMargin = statusBarHeight
            v.layoutParams = layoutParams
        } else if (v.layoutParams is LinearLayout.LayoutParams) {
            val layoutParams = v.layoutParams as LinearLayout.LayoutParams
            layoutParams.topMargin = statusBarHeight
            v.layoutParams = layoutParams
        }
    }

    /**
     * 状态栏字体深色或亮色
     *
     * @param isDarkFont     true 深色 文字颜色  true 黑色 false 白色
     * @param statusBarColor 状态栏背景颜色
     */
    fun setBarColor(activity: Activity, statusBarColor: Int, isDarkFont: Boolean) {
        ImmersionBar.with(activity)
            .statusBarColor(statusBarColor)
            .statusBarDarkFont(isDarkFont)
            .fitsSystemWindows(true) //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
            .init()
    }

}